<template>
  <div class="list-container">
    <vab-query-form>
      <el-form
        ref="form"
        :model="queryForm"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item>
          <el-input
            v-model.trim="queryBean.floorName"
            placeholder="项目名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>
    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
    >
      <el-table-column
        show-overflow-tooltip
        label="开发商"
        prop="propertyName"
        width="100px"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="floorName"
        label="项目名称"
        width="100px"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="开盘时间"
        prop="base.openingQuotation"
        width="200px"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="装修情况"
        prop="base.renovationCondition"
        width="100px"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="建筑类别"
        prop="base.buildingCategory"
        width="100px"
      ></el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        label="物业类型"
        prop="base.propertyType"
        width="100px"
      ></el-table-column> -->
      <el-table-column
        show-overflow-tooltip
        label="售楼地址"
        prop="base.salesAddress"
        width="200px"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="项目地址"
        prop="base.address"
        width="200px"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="销售状态"
        prop="estateStatus"
        width="100px"
      >
        <template #default="{ row }">
          <el-tag
            size="small"
            :type="
              row.estateStatus == 1
                ? 'success'
                : row.estateStatus == 2
                ? 'danger'
                : row.estateStatus == 0
                ? 'info'
                : 'warning'
            "
          >
            {{ getValue(row.estateStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="300px">
        <template #default="{ row }">
          <el-button type="primary" @click="handleEdit(row)">编辑</el-button>
          <el-button type="primary" @click="handleMapTree(row)">绑定</el-button>
          <el-button type="primary" @click="handleSet(row)">额外信息</el-button>
          <el-button type="primary" @click="handleHouseType(row)">
            户型
          </el-button>
          <!-- <el-button type="danger" @click="handleDelete(row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <extra-info ref="set" @fetch-data="fetchData"></extra-info>
    <!-- <add-house-type ref="house" @fetch-data="fetchData"></add-house-type> -->
    <house-type-manage ref="house" @fetch-data="fetchData"></house-type-manage>
    <set-map-tree ref="tree" @fetch-data="fetchData"></set-map-tree>
    <edit-building ref="edit" @fetch-data="fetchData"></edit-building>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>
<script>
  import { fetchBuildingList, deleteBuildingEstate } from '@/api/building'
  import ExtraInfo from './components/ExtraInfo.vue'
  import SetMapTree from './components/SetMapTree.vue'
  import AddHouseType from './components/AddHouseType.vue'
  import HouseTypeManage from './components/HouseTypeManage.vue'
  import EditBuilding from './components/EditBuilding.vue'
  export default {
    name: 'BuildingList',
    components: { ExtraInfo, SetMapTree, HouseTypeManage, EditBuilding },
    data() {
      return {
        list: [],
        imageList: [],
        listLoading: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: {},
        },
        queryBean: { floorName: '' },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    mounted() {},
    methods: {
      getValue(value) {
        switch (value) {
          case 0:
            return '未开售'
          case 1:
            return '销售中'
          case 2:
            return '已售罄'
        }
      },
      //自定义maptree
      handleMapTree(row) {
        this.$refs['tree'].showEdit(row.floorId)
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleSet(row) {
        this.$refs['set'].showEdit(row)
      },
      handleHouseType(row) {
        this.$refs['house'].showEdit(row)
      },
      handleDelete(row) {
        if (row.floorId) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { data } = await deleteBuildingEstate({
              floorId: row.floorId,
            })
            if (data.status == 0) {
              this.$baseMessage('删除成功！', 'success')
            } else {
              this.$baseMessage(data.message, 'error')
            }
            this.fetchData()
          })
        } else {
          // this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        if (this.queryBean.floorName == '') {
          this.queryForm.queryBean = {}
        } else {
          this.queryForm.queryBean = this.queryBean
        }
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        // const
        const { data } = await fetchBuildingList(this.queryForm)
        const { body, message } = data
        if (message == '成功') {
          const { data, totalSize } = body
          this.total = totalSize
          this.list = data
          this.$baseNotify('数据获取完毕！', '成功', 'success', 'top-right')
          console.log(data)
        }
        this.listLoading = false
      },
    },
  }
</script>
